package com.example.intentfiltertest;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class IntentFilterTestActivity extends ListActivity {
	
	private static final int PICK_CONTACT = 0;
	
	private static String [] actions =
	{
		"Open browser", "View contact", "Edit contact", "Google Maps",
		"Call phone", "Dial phone", "Insert contact", "Pick contacts",
		"Set wallpaper", "Websearch Maria", "Search Maria",
		"My Action + Category 1", "My Action + Category 1 & 2",
		"Broadcast message"
	};
	
	private static final String MY_ACTION = "com.example.intentfilter.nativesubst.ACTION";
	private static final String MY_CATEG1 = "com.example.intentfilter.nativesubst.CATEG1";
	private static final String MY_CATEG2 = "com.example.intentfilter.nativesubst.CATEG2";

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, actions);
        setListAdapter(adapter);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
        
        try {
			Uri uri = null;
			Intent intent = null;
			
			switch (position) {
			
			case 0:
				uri = Uri.parse("http://www.android.com");
				intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
				break;
			case 1:
				uri = Uri.parse("content://com.android.contacts/contacts/1");
				intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
				break;
			case 2:
				uri = Uri.parse("content://com.android.contacts/contacts/1");
				intent = new Intent(Intent.ACTION_EDIT, uri);
				startActivity(intent);
				break;
			case 3:
				uri = Uri.parse("geo:0,0?q=Presidente+Vargas,Rio+de+Janeiro");
				intent = new Intent(Intent.ACTION_VIEW, uri);	 // android.permission.INTERNET
				startActivity(intent);
				break;
			case 4:
				uri = Uri.parse("tel:12345678");
				intent = new Intent(Intent.ACTION_CALL, uri);	// android.permission.CALL_PHONE
				startActivity(intent);
				break;
			case 5:
				uri = Uri.parse("tel:12345678");
				intent = new Intent(Intent.ACTION_DIAL, uri);
				startActivity(intent);
				break;
			case 6:
				uri = Uri.parse("content://com.android.contacts/contacts");
				intent = new Intent(Intent.ACTION_INSERT, uri);
				startActivity(intent);
				break;
			case 7:
				intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
				startActivityForResult(intent, PICK_CONTACT);
				break;
			case 8:
				intent = new Intent(Intent.ACTION_SET_WALLPAPER);
				startActivity(intent);
				break;
			case 9:
				intent = new Intent(Intent.ACTION_WEB_SEARCH);
				intent.putExtra(SearchManager.QUERY, "Maria");
				startActivity(intent);
				break;
			case 10:
				intent = new Intent(Intent.ACTION_SEARCH);
				intent.putExtra(SearchManager.QUERY, "Maria");
				startActivity(intent);
				break;
			case 11:
				intent = new Intent(MY_ACTION);
				intent.addCategory(MY_CATEG1);
				startActivity(intent);
				break;
			case 12:
				intent = new Intent(MY_ACTION);
				intent.addCategory(MY_CATEG1);
				intent.addCategory(MY_CATEG2);
				startActivity(intent);
				break;
			case 13:
				intent = new Intent("com.example.broadcastreceiver.ACTION");
				sendBroadcast(intent);
				break;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(this.getClass().getName(),e.getMessage(), e);
		}
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	super.onActivityResult(requestCode, resultCode, data);
    	
    	if (requestCode == PICK_CONTACT && data != null) {
    		Uri uri = data.getData();
    		Log.i(this.getClass().getCanonicalName(), uri.toString());
    	}
    }
}
